//
//  DHYuYueController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/25.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHYuYueController.h"
#import "QBImagePickerController.h"
#import "UploadImageModel.h"

@interface DHYuYueController ()<QBImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (strong, nonatomic) NSString *url;

@end

@implementation DHYuYueController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"预约穿搭顾问";
    
    [self.imgView bk_whenTapped:^{
        UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册中选择", nil];
        // 显示
        [actionsheet showInView:self.view];
    }];
}

- (IBAction)yuyueAction:(id)sender {
    if (self.url && self.url.length > 0) {
        [[DHNetAPIClient shareDHNetAPIClient] request_consultParams:@{@"pic":self.url} andBlock:^(id data, NSError *error) {
            [NSObject showHudTipStr:@"预约成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }else{
        [NSObject showHudTipStr:@"请先添加照片"];
    }
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        
    }else if (buttonIndex == 1){
        QBImagePickerController *imagePickerController = [[QBImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        imagePickerController.maximumNumberOfSelection = 1;
        imagePickerController.filterType = QBImagePickerControllerFilterTypePhotos;
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:imagePickerController];
        [self presentViewController:navigationController animated:YES completion:NULL];
    }
}

#pragma mark - QBImagePickerControllerDelegate
- (void)imagePickerController:(QBImagePickerController *)imagePickerController didSelectAsset:(ALAsset *)asset{
    [self dismissImagePickerController];
}

- (void)imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController{
    [self dismissImagePickerController];
}

- (void)imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
    NSMutableArray *imgs = [NSMutableArray array];
    [assets enumerateObjectsUsingBlock:^(ALAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        CGImageRef iref = [rep fullScreenImage];
        if (iref) {
            UIImage *img = [UIImage imageWithCGImage:iref];
            [imgs addObject:img];
        }
    }];
    if (imgs.count > 0) {
        [self uploadImage:[imgs objectAtIndex:0]];
    }
    [self dismissImagePickerController];
}

- (void)uploadImage:(UIImage *)img{
    [[GlobalManager shareGlobalManager] upyunUploadImage:img andBlock:^(id data, NSError *error) {
        self.url = data;
        [self.imgView dh_setImageURLString:self.url];
    }];
}

- (void)dismissImagePickerController{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
